package com.example.recordingpen_datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import com.example.recordingpen_datacollection.entity.Vo.VoiceUse;
import com.example.recordingpen_datacollection.entity.VoiceConversion;
import com.example.recordingpen_datacollection.entity.VoiceLogin;
import com.example.recordingpen_datacollection.mapper.VoiceConversionMapper;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.example.recordingpen_datacollection.service.VoiceConversionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.recordingpen_datacollection.service.VoiceLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2024-08-07
 */
@Service
public class VoiceConversionServiceImpl extends ServiceImpl<VoiceConversionMapper, VoiceConversion> implements VoiceConversionService {
    @Autowired
    private VoiceLoginService voiceLoginService;

    @Override
    @Transactional
    public boolean setVoiceUse(VoiceLogin byId, VoiceUse voiceUse) {
        Integer dosage = getVoiceUse(byId.getLogId());

        Integer vlSumdosage = byId.getVlSumdosage();//总额的
        Integer useDosage = byId.getUseDosage();//使用额度
        int residue = vlSumdosage - dosage; //剩余
        if(voiceUse.getUsed()>residue){
            return false;
        }
        VoiceLogin voiceLogin = new VoiceLogin();
        voiceLogin.setVlId(byId.getVlId());
        Integer residue2 = byId.getUseDosage() + voiceUse.getUsed();
        voiceLogin.setUseDosage(residue2);
        boolean update = voiceLoginService.updateById(voiceLogin);
        if(!update){
            throw new JWException(500,"数据修改异常");
        }
        VoiceConversion voiceConversion = new VoiceConversion();
        voiceConversion.setVType(voiceUse.getServerType());
        voiceConversion.setVDosage(voiceUse.getUsed());
        voiceConversion.setUseDevid(byId.getLogId());
        boolean save = this.save(voiceConversion);
        if(!save){
            throw new JWException(500,"数据添加异常");
        }
        return true;
    }

    @Override
    public Integer getVoiceUse(Integer value) {
        String time = TimeJudgeUtils.addNowDayTime(0);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("use_devid",value);
        wrapper.ge("uaddtime",time+" 00:00:00");
        wrapper.le("uaddtime",time+" 23:59:59");
        List<VoiceConversion> list = this.list(wrapper);
        if (list == null || list.size() <=0){
            return 0;
        }
        Integer dosage = 0;
        for (VoiceConversion voiceConversion : list) {
            Integer vDosage = voiceConversion.getVDosage();
            dosage+=vDosage;
        }
        return dosage;
    }
}
